import tensorflow as tf
import os

'''
多输出感知机
    全连接，将4个参数，映射到3维度空间
'''
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

x = tf.random.normal([2, 4])
w = tf.random.normal([4, 3])
b = tf.zeros([3])
y = tf.constant([2, 0])
print('x: \n{}'.format(x.numpy()))
print('w: \n{}'.format(w.numpy()))
print('b: \n{}'.format(b.numpy()))
print('y: \n{}'.format(y.numpy()))
print('y of one hot code: \n{}'.format(tf.one_hot(y, depth=3)))

with tf.GradientTape() as tape:
    tape.watch([w, b])
    prob = tf.nn.softmax(x @ w + b)
    loss = tf.reduce_mean(tf.losses.MSE(tf.one_hot(y, depth=3), prob))

grad = tape.gradient(loss, [w, b])
print(grad)
